package uni.lpoo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import uni.lpoo.dto.ArticuloDTO;
import uni.lpoo.dto.cuentasDTO;
import uni.lpoo.tool.Conexion;

public class cuentasDAO {
     public ArrayList listCuentas() throws Exception  {
        ArrayList list = null;
        String sql = "Select * from cuentas";
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/contabilidad";
        String user = "root";
        String pass = "root";
        
        Conexion conex = new Conexion(driver, url, user, pass);
        Connection cn = conex.getConnectionX();
        Statement stmt = cn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            cuentasDTO bean = new cuentasDTO();
            bean.setNumCuenta(rs.getInt("cod_cuent"));
            bean.setNomCuenta(rs.getString("nom_cuenta"));
            list.add(bean);
        }

        return list;
    }
    public int insertArticulo(cuentasDTO articulo) throws Exception {
        ArrayList list = null;
        String sql = "Insert into cuentas values(?,?)";
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/contabilidad";
        String user = "fiis";
        String pass = "java";
        Conexion conex = new Conexion(driver, url, user, pass);
        Connection cn = conex.getConnectionX();
        PreparedStatement pstmt = cn.prepareStatement(sql);
        pstmt.setInt(1, articulo.getNumCuenta());
        pstmt.setString(2, articulo.getNomCuenta());
        int n = pstmt.executeUpdate();
        //stmt.executeUpdate(s);
        return n;
    }

}
